package com.algorithm;

import com.leetcode.solution.model.LeftRightNode;

import java.util.ArrayDeque;
import java.util.Queue;

public class Queue2Tree {
    public static void main(String[] args) {
        Integer[] vals = new Integer[] {1,2,3, null,null,6,7};
        Queue<LeftRightNode> queue = new ArrayDeque<>();
        LeftRightNode curr = null;
        LeftRightNode root = null;
        for (int i = 0; i < vals.length; i++) {
            LeftRightNode node = new LeftRightNode(vals[i]);
            if (root == null) {
                root = node;
            }

            curr = queue.peek();
            queue.add(node);
            if (curr == null) {
                continue;
            } else if (curr.left == null) {
                curr.left = node;
            } else if (curr.right == null) {
                curr.right = node;
                queue.remove();
            }
        }

        printTree(root);
    }

    private static void printTree(LeftRightNode root) {
        if (root == null) {
            return;
        }

        System.out.println(root.val);
        printTree(root.left);
        printTree(root.right);
    }
}
